package com.LeeCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 与车相交的点
 */

public class Code2848 {
    public static void main(String[] args) {
        List<List<Integer>> nums = new ArrayList<>();
        nums.add(Arrays.asList(3, 6));
        nums.add(Arrays.asList(1, 5));
        nums.add(Arrays.asList(4, 7));
        System.out.println(new Code2848().numberOfPoints(nums));
    }

    public int numberOfPoints(List<List<Integer>> nums) {
        int[] diff = new int[102];  // 差分数组，覆盖范围 1~100，预留到 101
        for (List<Integer> row : nums) {
            int start = row.get(0), end = row.get(1);
            diff[start]++;       // 开始：+1
            diff[end + 1]--;     // 结束后：-1
        }
        int ans = 0, sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += diff[i];      // 前缀和：当前点被覆盖的次数
            if (sum != 0) ans++; // 被覆盖至少一次
        }
        return ans;
    }
}
